var orgSelecter = {
	initOrgSelecter:function(onSelected,scope, fullid){
		var boss = this;
		boss.scope = scope;
		boss.fullid = fullid;
		boss.onSelected = onSelected;
		boss.firstLoad;
		if(boss.fullid != null){
			boss.initOrg();
		}
		boss.initMcEvent();
		boss.initImageUnderEvent();
	},
	initMcEvent : function(){
		var boss = this;
		
		$(".mc").bind("click", function(){
			$(this).parent().nextAll().hide();
			$("div[imagesize]").removeAttr("selected");
			$(this).parent().nextAll("div[tag]").removeAttr("selected");
			if(boss.onSelected){
				boss.onSelected.call(boss.scope);
			}
		});
	},
	initImageUnderEvent : function(){
		var boss = this;
		$("div[imagesize]").bind("click", function(){
			var num = $(this).attr("imagesize");
			var selected = $(this).attr("selected");
			if(selected){
				$("div[under]").hide();
				$(this).removeAttr("selected");
				$("div[imagesize='" + num + "'] img").attr("src", burl + "/images/10.png");
			} else {
				$(this).attr("selected", "selected");
				$("div[under]").hide();
				$("div[imagesize='" + num + "']").next().show();
				var div = $(this).parents("div[tag]").siblings("div[tag]");
				for(var i = 0; i < div.length; i++){
					$(div[i]).find("div[imagesize]").removeAttr("selected");
					$(div[i]).find("div[imagesize]").children().attr("src", burl + "/images/10.png");
				}
				$("div[imagesize='" + num + "'] img").attr("src", burl + "/images/11.png");
				var div = $(this).parents("div[tag]").children("div[under]");
				var pid = $(this).parents("div[tag]").children(".mc").attr("pid");
				if(pid == "1" && boss.firstLoad){
					return;
				}
				var isstreet = $(this).parents("div[tag]").children(".mc").attr("isstreet");
				boss.loadOrg(div, pid,isstreet);
			}
		});
	},
	TableTdEvent : function(){
		var boss = this; 
		$(".all-org-select td").unbind("mouseenter").bind("mouseenter", function(){
			$("#all-org-select td").removeClass("selected");
			$(this).addClass("selected");
		});
		$(".all-org-select td").unbind("mouseleave").bind("mouseleave", function(){
			$(this).removeClass("selected");
		});
		$(".all-org-select td").unbind("click").bind("click", function(){
			var text = $(this).html();
			var isstreet = $(this).attr("isstreet");
			var pid = $(this).attr("deptid");
			var fullid = $(this).attr("fullid");
			var mapx = $(this).attr("mapx");
			var mapy = $(this).attr("mapy");
			$("div[under]").hide();
			$(this).parents("div[tag]").nextAll("div[tag]").hide();
			$(this).parents("div[under]").prev().removeAttr("selected");
			$(this).parents("div[under]").prev().children().attr("src", burl + "/images/10.png");
			$(this).parents("div[tag]").next().children(".mc").attr("pid", pid);
			$(this).parents("div[tag]").next().attr("pid", pid).attr("fullid",fullid).attr("mapx",mapx).attr("mapy",mapy);
			$(this).parents("div[tag]").next().children(".mc").html(text).attr("isstreet",isstreet);
			$(this).parents("div[tag]").next().attr("selected", "selected").attr("isstreet",isstreet);
			$(this).parents("div[tag]").next().show();
			if(boss.onSelected){
				boss.onSelected.call(boss.scope);
			}
		});
		$(".close img").unbind("click").bind("click", function(){
			$(this).parents("div[under]").hide();
			$(this).parents("div[under]").prev().removeAttr("selected");
			$(this).parents("div[under]").prev().children().attr("src", burl + "/images/10.png");
		});
	},
	TableEvent : function(){
		$("div[under]").bind("mouseleave", function(){
			$(this).hide();
			$(this).prev().removeAttr("selected");
			$(this).prev().children().attr("src", burl + "/images/10.png");
		});
	},
	loadOrg : function(div, pid,isstreet){
		var boss = this;
		var url = "";
		if(isstreet == "1"){
			url = burl + '/StreetDept/getDept.do?id=' + pid;
		}
		else{
			url = burl + '/Dept/getDept.do?id=' + pid;
		}
		
		$.ajax({
			dataType: 'json',
			type: 'post',
			url: url,
			success: function(data){
				boss.firstLoad = "1";
				var htmlContent = "<table class='all-org-select'>";
				var rowlength = Math.ceil(data.length/4);
				for(var i = 0; i < rowlength; i++){
					htmlContent += "<tr>";
					for(var j = 0; j < 4; j++){
						var row = data[i*4+j];
						if(!row){
							break;
						}
						var deptname = row.deptname;
						var deptid = row.id;
						var mapx = row.mapx;
						var mapy = row.mapy;
						var fullid = row.fullid;
						var isstreet = row.isstreet;
						htmlContent += "<td mapx='"+mapx+"' isstreet='"+isstreet+"' mapy='"+mapy+"' fullid='"+fullid+"' deptid='"+deptid+"'>" + deptname +"</td>";
					}
					htmlContent += "</tr>";
				}
				htmlContent += "</table>";
				$("div[ajaxUnderOrg]", div).html(htmlContent);
				if(pid == "1"){
					boss.loadStreetOrg(div, pid);
				}
				else{
					boss.TableTdEvent();
				}
			}
		});
	},
	loadStreetOrg:function(div, pid){
		var boss = this;
		
		$.ajax({
			dataType: 'json',
			type: 'post',
			url: burl + '/StreetDept/getDept.do?id=1',
			success: function(data){
				var rowlength = Math.ceil(data.length/4);
				$("table.all-org-select", div).append("<tr><td colspan='4'>————————————————————————————————————</td></tr>");
				for(var i = 0; i < rowlength; i++){
					var htmlContent = "<tr>";
					for(var j = 0; j < 4; j++){
						var row = data[i*4+j];
						if(!row){
							break;
						}
						var deptname = row.deptname;
						var deptid = row.id;
						var mapx = row.mapx;
						var mapy = row.mapy;
						var fullid = row.fullid;
						htmlContent += "<td mapx='"+mapx+"' isstreet='1' mapy='"+mapy+"' fullid='"+fullid+"' deptid='"+deptid+"'>" + deptname +"</td>";
					}
					htmlContent += "</tr>";
					$("table.all-org-select", div).append($(htmlContent));
				}
				
				boss.TableTdEvent();
			}
		});
	},
	initOrg : function(){
		var boss = this;
		var fullid = boss.fullid;
		$.ajax({
			dataType: 'json',
			type: 'post',
			data: {fullid: fullid},
			url: burl + '/Dept/getInitOrg.do',
			success: function(data){
				for(var i = 1; i < data.length; i++){
					var row = data[i];
					$("#xuanxiangka div[tag='z" + (i+1) + "']").show();
					$("#xuanxiangka div[tag='z" + (i+1) + "']").attr("selected", "selected");
					$("#xuanxiangka div[tag='z" + (i+1) + "']").attr("pid", row.ID);
					$("#xuanxiangka div[tag='z" + (i+1) + "']").attr("fullid", row.FULLID);
					$("#xuanxiangka div[tag='z" + (i+1) + "']").find(".mc").attr("pid", row.ID);
					$("#xuanxiangka div[tag='z" + (i+1) + "']").find(".mc").html(row.DEPTNAME);
				}
			}
		});
	},
	getPaichusuoFullid:function(){
		var paichusuoFullid = $("#xuanxiangka div[tag='z2'][selected]").attr("fullid");
		if(!paichusuoFullid || paichusuoFullid == ""){
			return "0-1-";
		}
		return paichusuoFullid;
	},
	getJingquFullid:function(){
		var boss = this;
		var jingquFullid = $("#xuanxiangka div[tag='z3'][selected]").attr("fullid");
		
		if(!jingquFullid || jingquFullid ==""){
			return boss.getPaichusuoFullid();
		}
		return jingquFullid;
	},
	getZerenquFullid:function(){
		var boss = this;
		var zerenquFullid = $("#xuanxiangka div[tag='z4'][selected]").attr("fullid");
		
		if(!zerenquFullid || zerenquFullid ==""){
			return boss.getJingquFullid();
		}
		return zerenquFullid;
	},
	getSelectLevel:function(){
		 var tag = $("#xuanxiangka div[tag^='z'][selected]").last().attr("tag");
		 if(tag == "z1"){
			 return 1;
		 }
		 else if(tag == "z2"){
			 return 2;
		 }
		 else if(tag == "z3"){
			 return 3;
		 }
		 else if(tag == "z4"){
			 return 4;
		 }
	}
};